package com.example.hotelsystem.controller.room;


import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelsystem.entity.Hotelorder;
import com.example.hotelsystem.entity.Room;
import com.example.hotelsystem.entity.RoomType;
import com.example.hotelsystem.entity.User;
import com.example.hotelsystem.page.Pagesize;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.server.Server;
import com.example.hotelsystem.service.RoomTypeService;
import com.example.hotelsystem.vo.roomtype.RoomTypeSaveVO;
import com.example.hotelsystem.vo.roomtype.RoomTypeViewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/hotelsystem/RoomType")
@Api(tags = "房间类型控制器")
public class RoomTypeController {

    @Autowired
    RoomTypeService RoomTypeService;

    @ApiOperation("房间类型详情接口")
    @PostMapping("/view")
    public Result RoomTypeView(Long roomTypeId){
        try{
            RoomTypeViewVO vo = RoomTypeService.getRoomTypeView(roomTypeId);
            return Result.success(vo);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("获取房间类型列表接口")
    @PostMapping("/list")
    public Result RoomTypeList(@RequestParam(defaultValue = "1") Integer currentPage){
        QueryWrapper<RoomType> query = new QueryWrapper<>();
        query.eq("is_delete",0);
        Page page = new Page(currentPage, Pagesize.pageSize1);
        IPage<RoomType> list=RoomTypeService.page(page,query);
        if(list!=null){
            return Result.success("获取成功",list);
        }
        return Result.error(CodeError.ROOM_TYPE_LIST_NULL,"没有找到房间类型列表，😭");
    }

    @ApiOperation("后台房间类型获取接口")
    @PostMapping("/getView")
    public Result getViewById(Long id){
        try {
            RoomType roomType = RoomTypeService.getById(id);
            return Result.success(roomType);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("添加房间类型接口")
    @PostMapping("/add")
    public Result AddRoomType(@Valid RoomTypeSaveVO roomTypeSaveVO,BindingResult result){
        if (result.hasErrors()) {
            return Result.error(result.getFieldError().getDefaultMessage());
        }

        RoomType roomType = new RoomType();
        BeanUtils.copyProperties(roomTypeSaveVO,roomType);
        boolean isAdd = RoomTypeService.save(roomType);
        if(isAdd){
            return Result.success("添加成功");
        }
        return Result.error(CodeError.ROOM_TYPE_SAVE_FAIL,"房间类型添加失败😭");
    }

    @ApiOperation("删除房间类型接口")
    @PostMapping("/remove")
    public Result RemoveRoomType(@RequestParam("id")Long id){
        RoomType roomType = RoomTypeService.getById(id);
        roomType.setIsDelete(1);
        boolean isRemove = RoomTypeService.updateById(roomType);
        if(isRemove){
            return Result.success("删除成功");
        }
        return Result.error(CodeError.ROOM_TYPE_REMOVE_FAIL,"房间类型删除失败😭");
    }


    @ApiOperation("修改房间类型信息接口")
    @PostMapping("/mod")
    public Result ModifyRoomType(RoomType RoomType){
        boolean isUpdate = RoomTypeService.updateById(RoomType);
        if(isUpdate){
            return Result.success("房间类型信息修改成功");
        }else{
            return Result.error(CodeError.ROOM_TYPE_MOD_FAIL,"房间类型信息修改失败,😭");
        }
    }

    @ApiOperation("房间类型上传图片得到图片url")
    @PostMapping("/uploadImg")
    public Result UserUploadImg(MultipartFile file){
        if (!file.isEmpty()) {
            //对文文件的全名进行截取然后在后缀名进行删选。
            int begin = file.getOriginalFilename().indexOf(".");
            int last = file.getOriginalFilename().length();
            //获得文件后缀名
            String a = file.getOriginalFilename().substring(begin, last);
            //判断是否为图片
            if ((".gif").equals(a)||(".GIF").equals(a)||
                    (".jpg").equals(a)||(".JPG").equals(a)||
                    (".PNG").equals(a)||(".png").equals(a)){
                return RoomTypeService.upLoadImg(file);
            }else {
                return Result.error("请上传png、jpg格式的图片文件");
            }
        }else{
            return Result.error("空文件");
        }


    }
}

